package com.example.controller;

import com.example.common.Result;
import com.example.entity.ItemGroup;
import com.example.service.ItemGroupService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/itemGroup")
public class ItemGroupController {
    @Resource
     private ItemGroupService itemGroupService;




    @RequestMapping(value = "/itemGroupSave",method = {RequestMethod.POST})
    public Result<?> itemGroupSave(String itemids,@RequestBody ItemGroup itemGroup){
        System.out.println("itemids = " + itemids);
        System.out.println("itemGroup = " + itemGroup);
        itemGroupService.add(itemids,itemGroup);
        return null;
    }

    @PostMapping
    public Result<?> save(@RequestBody ItemGroup itemGroup) {
        return Result.success(itemGroupService.save(itemGroup));
    }

    @PutMapping
    public Result<?> update(@RequestBody ItemGroup itemGroup) {
        return Result.success(itemGroupService.updateById(itemGroup));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        itemGroupService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<ItemGroup> findById(@PathVariable Long id) {
        return Result.success(itemGroupService.getById(id));
    }

    @GetMapping
    public Result<List<ItemGroup>> findAll() {
        return Result.success(itemGroupService.list());
    }

    @GetMapping("/page")
    public Result<IPage<ItemGroup>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(itemGroupService.page(new Page<>(pageNum, pageSize), Wrappers.<ItemGroup>lambdaQuery().like(ItemGroup::getName, name)));
    }

}